package com.me.pikachu.main;

import java.util.ArrayList;
import java.util.Stack;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class MainPikachu implements ApplicationListener, InputProcessor {

	private OrthographicCamera camera;

	private SpriteBatch batch;

	private MatrixTransformScreen transferMatrix;

	private MatrixProcess processMatrix;

	private DrawObject drawObjects;

	private ArrayList<Point> destSource;

	@Override
	public void create() {
		float w = Gdx.graphics.getWidth();
		float h = Gdx.graphics.getHeight();
		Gdx.input.setInputProcessor(this);

		camera = new OrthographicCamera(800, 800 * h / w);
		camera.position.set(camera.viewportWidth / 2,
				camera.viewportHeight / 2, 0);
		batch = new SpriteBatch();

		Asset.loadAssest();
		while (!Asset.manager.update())
			;
		Asset.getPokemon();
		Asset.getSquares();

		transferMatrix = new MatrixTransformScreen(camera, 4, 5);
		processMatrix = new MatrixProcess();
		drawObjects = new DrawObject(batch, camera, transferMatrix,
				processMatrix);

		destSource = new ArrayList<Point>();
	}

	@Override
	public void dispose() {
		batch.dispose();
	}

	@Override
	public void render() {
		Gdx.gl.glClearColor(1, 1, 1, 1);
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

		drawObjects.draw();

		// batch.setProjectionMatrix(camera.combined);
		// batch.begin();
		// sprite.draw(batch);
		// batch.end();
	}

	@Override
	public void resize(int width, int height) {
	}

	@Override
	public void pause() {
	}

	@Override
	public void resume() {
	}

	@Override
	public boolean keyDown(int keycode) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean keyUp(int keycode) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean keyTyped(char character) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean touchDown(int screenX, int screenY, int pointer, int button) {

		Point p = transferMatrix.Screen2Point(screenX, screenY);

		if (p != null) {

			p = new Point(p.i, p.j, processMatrix.getOrigialMatrix());
			if (!p.isComparePoint()) {

				if (destSource.size() == 0)
					destSource.add(p);
				else {
					for (int i = 0; i < destSource.size(); i++) {
						Point first = destSource.get(i);
						if ((p.i != first.i) || (p.j != first.j)) {
							destSource.add(p);
						}
					}
				}

				if (destSource.size() == 2) {
					removePokemon(destSource.get(0), destSource.get(1));
					destSource.clear();
				}

				System.out.println(destSource.size());
			}
		}
		return false;
	}

	@Override
	public boolean touchUp(int screenX, int screenY, int pointer, int button) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean touchDragged(int screenX, int screenY, int pointer) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean mouseMoved(int screenX, int screenY) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean scrolled(int amount) {
		// TODO Auto-generated method stub
		return false;
	}

	private void removePokemon(Point first, Point second) {

		first = new Point(first.i + 1, first.j + 1,
				processMatrix.getNewMatrix());
		second = new Point(second.i + 1, second.j + 1);
		Stack<Point> path = first.getPath(second);
		while (!path.isEmpty()) {
			Point p = path.pop();
			Gdx.app.log("MAIN", "message: " + p.i + ", " + p.j);
		}
		processMatrix.setMatrix(first, second);
		drawObjects.changeOriginalMatrix();
	}
}
